/**
 * Copyright (c) 2021 sdpjw Inc. All rights reserved.
 */
package com.lmn.oval;


import net.sf.oval.ValidationCycle;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.exception.OValException;

/**
 * @version 4.0
 * @describe:
 * @author: houkai
 * @Date: 2021/06/03
 */
public class DraftNoCheckImpl extends AbstractAnnotationCheck<DraftNoCheck> {

    @Override
    public void configure(DraftNoCheck draftNoCheck) {
        super.configure(draftNoCheck);
    }

    /**
     * 判断票号是否有效
     *
     * @param validatedObject 进入校验器的对象
     * @param valueToValidate 注解当前标注的属性值
     * @return 是否通过验证
     * @throws OValException OValException
     */
    @Override
    public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final ValidationCycle cycle) throws OValException {
        if (validatedObject == null) {
            return true;
        }
        String value = (String) valueToValidate;
        if (value.length() != 30) {
            return false;
        }
        int m = 10;
        int s = 0;
        int p = 0;
        for (int i = 0; i < value.length(); i++) {
            int item = value.charAt(i) - 48;
            if (item >= 10) {
                item = item - 7;
            }
            s = p % (m + 1) + item;
            p = (s % m == 0 ? m : s % m) * 2;
        }
        return s % m == 1;
    }

}
